<template>
	<view>
		<CouponListNavbarSwiper
			ref="navbarSwiper"
			:navbarList="navbarList" 
			:curNavbar="curNavbar" 
			:height="'88rpx'"
			:dateList="dateList"
			@handleChooseNavbar="handleChooseNavbar"
			@handleRefresherrefresh="handleRefresherrefresh"
		>
			<!-- @handleScrolltolower="handleScrolltolower" -->
		</CouponListNavbarSwiper>
	</view>
</template>

<script>
	import CouponListNavbarSwiper from '@/components/home/coupon-navbarSwiper.vue'
	export default {
		components: {
			CouponListNavbarSwiper
		},
		data() {
			return {
				navbarList: [
					{name: '通用',alias: 'general'},
					{name: '交通',alias: 'traffic'},
					{name: '景区',alias: 'scenic'},
					{name: '酒店',alias: 'hotel'},
					{name: '购物',alias: 'shop'},
				],
				curNavbar: {name: '通用',alias: 'general'},
				curIndex: 0,
				generalList: [],
				trafficList: [],
				scenicList: [],
				hotelList: [],
				shopList: [],
				page: {
					pageNum: 0,
					totalPageNum: 0,
					pageSize: 10,
				},
			};
		},
		onLoad() {
			this.getCouponList()
			uni.$on('getCopponSuccess', (data) => { // 领取优惠券成功
				this.getCouponList()
			})
		},
		computed: {
			dateList() {
				return [this.generalList, this.trafficList, this.scenicList, this.hotelList, this.shopList]
			}
		},
		methods: {
			handleChooseNavbar(item, i) {
				this.curNavbar = item
				this.curIndex = i
				this.getCouponList()
			},
			handleScrolltolower(item, i) {
				let totalPage = Math.ceil(this.page.totalPageNum / this.page.pageSize)
				if (this.page.pageNum >= totalPage) return
				this.page.pageNum++
				this.getMoreCouponList()
			},
			handleRefresherrefresh(item, i) {
				this.page.pageNum = 1
				this.getCouponList()
			},
			getCouponList() {
				this.page.pageNum = 1
				this.$http.get(this.$api.homeCouponList,{type: this.curIndex + 1}).then(res => {
					if (res.code === 200) {
						this.page.totalPageNum = res.data.total
						this.$refs.navbarSwiper.stopPullDownRefresh()
						if (this.curIndex == 0) {
							this.generalList = res.data
						} else if (this.curIndex == 1) {
							this.trafficList = res.data
						} else if (this.curIndex == 2) {
							this.scenicList = res.data
						} else if (this.curIndex == 3) {
							this.hotelList = res.data
						} else if (this.curIndex == 4) {
							this.shopList = res.data
						}
					}
				})
			},
			getMoreCouponList() {
				this.page.pageNum = 1
				this.$http.get(this.$api.homeCouponList,{type: this.curIndex + 1}).then(res => {
					if (res.code === 200) {
						this.page.totalPageNum = res.data.total
						this.$refs.navbarSwiper.stopPullDownRefresh()
						if (this.curIndex == 0) {
							this.generalList = [...this.generalList, ...res.data]
						} else if (this.curIndex == 1) {
							this.trafficList = [...this.trafficList, ...res.data]
						} else if (this.curIndex == 2) {
							this.scenicList = [...this.scenicList, ...res.data]
						} else if (this.curIndex == 3) {
							this.hotelList = [...this.hotelList, ...res.data]
						} else if (this.curIndex == 4) {
							this.shopList = [...this.shopList, ...res.data]
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>

</style>
